#!/bin/sh
#
# Copyright (C) 2000-2023 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#

cwd=`pwd`
. scripts/functions
mkdir -p ${bin}
mkdir -p ${tmp}
out="${tmp}/sed_tmp"

${rscripts}/create_sed

# process .in files with sed script
#sed -f ${out} ${rscripts}/bacula-dir.conf.in >${rscripts}/bacula-dir.conf
#sed -f ${out} ${rscripts}/bacula-fd.conf.in >${rscripts}/bacula-fd.conf
#sed -f ${out} ${rscripts}/bacula-sd.conf.in >${rscripts}/bacula-sd.conf
#sed -f ${out} ${rscripts}/bconsole.conf.in >${rscripts}/bconsole.conf
sed -f ${out} ${rscripts}/win32-bacula-dir.conf.in >${rscripts}/win32-bacula-dir.conf
sed -f ${out} ${rscripts}/plugin-test-bacula-dir.conf.in >${rscripts}/plugin-test-bacula-dir.conf
sed -f ${out} ${rscripts}/plugin-handleXACL-test-bacula-dir.conf.in >${rscripts}/plugin-handleXACL-test-bacula-dir.conf
sed -f ${out} ${rscripts}/swift-plugin-test-bacula-dir.conf.in >${rscripts}/swift-plugin-test-bacula-dir.conf
sed -f ${out} ${rscripts}/rhv-plugin-test-bacula-dir.conf.in >${rscripts}/rhv-plugin-test-bacula-dir.conf

# Kubernetes plugin
sed -f ${out} ${rscripts}/kubernetes-plugin-test-bacula-dir.conf.in >${rscripts}/kubernetes-plugin-test-bacula-dir.conf
sed -f ${out} ${rscripts}/k8s_backend.in >${rscripts}/k8s_backend

find ${rscripts}/kubernetes/ -type f -name "*.conf.in" | while read -r file; do
  compiled_conf="${file%.in}"
  sed -f ${out} ${file} > ${compiled_conf}
done


sed -f ${out} ${rscripts}/openshift-plugin-test-bacula-dir.conf.in >${rscripts}/openshift-plugin-test-bacula-dir.conf
sed -f ${out} ${rscripts}/docker-plugin-test-bacula-dir.conf.in >${rscripts}/docker-plugin-test-bacula-dir.conf
sed -f ${out} ${rscripts}/bacula-sd-2disk-virtual.conf.in >${rscripts}/bacula-sd-2disk-virtual.conf
sed -f ${out} ${rscripts}/bacula-sd-virtual-tape.conf.in >${rscripts}/bacula-sd-virtual-tape.conf
sed -f ${out} ${rscripts}/bacula-dir.conf.errors.in >${rscripts}/bacula-dir.conf.errors
sed -f ${out} ${rscripts}/bacula-dir.conf.accurate.in >${rscripts}/bacula-dir.conf.accurate
sed -f ${out} ${rscripts}/test-bacula-dir.conf.in >${rscripts}/test-bacula-dir.conf
sed -f ${out} ${rscripts}/multi-client-bacula-dir.conf.in >${rscripts}/multi-client-bacula-dir.conf
sed -f ${out} ${rscripts}/bacula-dir.conf.regexwhere.in >${rscripts}/bacula-dir.conf.regexwhere
sed -f ${out} ${rscripts}/bacula-dir.conf.maxtime.in >${rscripts}/bacula-dir.conf.maxtime
sed -f ${out} ${rscripts}/bacula-dir.conf.maxruntime.in >${rscripts}/bacula-dir.conf.maxruntime
sed -f ${out} ${rscripts}/new-test-bacula-dir.conf.in >${rscripts}/new-test-bacula-dir.conf
sed -f ${out} ${rscripts}/testa-bacula-dir.conf.in >${rscripts}/testa-bacula-dir.conf
sed -f ${out} ${rscripts}/testb-bacula-dir.conf.in >${rscripts}/testb-bacula-dir.conf
sed -f ${out} ${rscripts}/test-bacula-fd.conf.in >${rscripts}/test-bacula-fd.conf
sed -f ${out} ${rscripts}/test-bacula-sd.conf.in >${rscripts}/test-bacula-sd.conf
sed -f ${out} ${rscripts}/aligned-bacula-sd.conf.in >${rscripts}/aligned-bacula-sd.conf
sed -f ${out} ${rscripts}/aligned-bacula-dir.conf.in >${rscripts}/aligned-bacula-dir.conf
sed -f ${out} ${rscripts}/migrate-bacula-dir.conf.in >${rscripts}/migrate-bacula-dir.conf
sed -f ${out} ${rscripts}/migrate-bacula-sd.conf.in >${rscripts}/migrate-bacula-sd.conf
sed -f ${out} ${rscripts}/virtualfull-extreme-bacula-dir.conf.in >${rscripts}/virtualfull-extreme-bacula-dir.conf
sed -f ${out} ${rscripts}/test-console.conf.in >${rscripts}/test-console.conf
sed -f ${out} ${rscripts}/crypto-bacula-fd.conf.in >${rscripts}/crypto-bacula-fd.conf
sed -f ${out} ${rscripts}/gpfs-bacula-dir.conf.in >${rscripts}/gpfs-bacula-dir.conf
sed -f ${out} ${rscripts}/bacula-dir-tape.conf.in >${rscripts}/bacula-dir-tape.conf
sed -f ${out} ${rscripts}/bacula-dir-2client.conf.in >${rscripts}/bacula-dir-2client.conf
sed -f ${out} ${rscripts}/bacula-dir-2media-virtual.conf.in >${rscripts}/bacula-dir-2media-virtual.conf
sed -f ${out} ${rscripts}/bacula-dir-fifo.conf.in >${rscripts}/bacula-dir-fifo.conf
sed -f ${out} ${rscripts}/bacula-dir-strip.conf.in >${rscripts}/bacula-dir-strip.conf
sed -f ${out} ${rscripts}/bacula-dir-migration.conf.in >${rscripts}/bacula-dir-migration.conf
sed -f ${out} ${rscripts}/bacula-dir-virtual.conf.in >${rscripts}/bacula-dir-virtual.conf
sed -f ${out} ${rscripts}/bacula-sd-tape.conf.in >${rscripts}/bacula-sd-tape.conf
sed -f ${out} ${rscripts}/bacula-sd-btape.conf.in >${rscripts}/bacula-sd-btape.conf
sed -f ${out} ${rscripts}/ansi-sd-tape.conf.in >${rscripts}/ansi-sd-tape.conf
sed -f ${out} ${rscripts}/bacula-sd-fifo.conf.in >${rscripts}/bacula-sd-fifo.conf
sed -f ${out} ${rscripts}/bacula-sd-2tape.conf.in >${rscripts}/bacula-sd-2tape.conf
sed -f ${out} ${rscripts}/bacula-sd-migration.conf.in >${rscripts}/bacula-sd-migration.conf
sed -f ${out} ${rscripts}/bacula-sd-virtual.conf.in >${rscripts}/bacula-sd-virtual.conf
sed -f ${out} ${rscripts}/bacula-sd-2disk.conf.in >${rscripts}/bacula-sd-2disk.conf
sed -f ${out} ${rscripts}/bacula-sd-4disk.conf.in >${rscripts}/bacula-sd-4disk.conf
sed -f ${out} ${rscripts}/bacula-sd-2media-virtual.conf.in >${rscripts}/bacula-sd-2media-virtual.conf
sed -f ${out} ${rscripts}/bacula-sd-2drive.conf.in >${rscripts}/bacula-sd-2drive.conf
sed -f ${out} ${rscripts}/bacula-sd-2disk-drive.conf.in >${rscripts}/bacula-sd-2disk-drive.conf
sed -f ${out} ${rscripts}/bacula-sd-2disk-virtual.conf.in >${rscripts}/bacula-sd-2disk-virtual.conf
sed -f ${out} ${rscripts}/broken-media-bug-bacula-dir.conf.in >${rscripts}/broken-media-bug-bacula-dir.conf
sed -f ${out} ${rscripts}/broken-media-bug-2-bacula-dir.conf.in >${rscripts}/broken-media-bug-2-bacula-dir.conf
sed -f ${out} ${rscripts}/broken-media-bug-2-bacula-sd.conf.in >${rscripts}/broken-media-bug-2-bacula-sd.conf
sed -f ${out} ${rscripts}/cleanup-tape.in >${rscripts}/cleanup-tape
sed -f ${out} ${rscripts}/cleanup-2tape.in >${rscripts}/cleanup-2tape
sed -f ${out} ${rscripts}/cleanup-2drive.in >${rscripts}/cleanup-2drive
sed -f ${out} ${rscripts}/prepare-two-tapes.in >${rscripts}/prepare-two-tapes
sed -f ${out} ${rscripts}/bacula-dir.conf.testrunscript.in >${rscripts}/bacula-dir.conf.testrunscript
sed -f ${out} ${rscripts}/bacula-dir-vtape.conf.in >${rscripts}/bacula-dir-vtape.conf
sed -f ${out} ${rscripts}/bacula-dir-vtape2.conf.in >${rscripts}/bacula-dir-vtape2.conf
sed -f ${out} ${rscripts}/bacula-sd-vtape.conf.in >${rscripts}/bacula-sd-vtape.conf
sed -f ${out} ${rscripts}/prepare-fake-autochanger.in >${rscripts}/prepare-fake-autochanger
sed -f ${out} ${rscripts}/tls-console.conf.in >${rscripts}/tls-console.conf
sed -f ${out} ${rscripts}/tls-bacula-fd.conf.in >${rscripts}/tls-bacula-fd.conf
sed -f ${out} ${rscripts}/tls-crypto-bacula-fd.conf.in >${rscripts}/tls-crypto-bacula-fd.conf
sed -f ${out} ${rscripts}/tls-bacula-sd.conf.in >${rscripts}/tls-bacula-sd.conf
sed -f ${out} ${rscripts}/tls-bacula-dir.conf.in >${rscripts}/tls-bacula-dir.conf

sed -f ${out} ${rscripts}/collect-bacula-dir.conf.in >${rscripts}/collect-bacula-dir.conf
sed -f ${out} ${rscripts}/collect-bacula-sd.conf.in >${rscripts}/collect-bacula-sd.conf
sed -f ${out} ${rscripts}/collect-bacula-fd.conf.in >${rscripts}/collect-bacula-fd.conf

sed -f ${out} ${rscripts}/tls-auth-bacula-fd.conf.in >${rscripts}/tls-auth-bacula-fd.conf
sed -f ${out} ${rscripts}/tls-auth-bacula-sd.conf.in >${rscripts}/tls-auth-bacula-sd.conf
sed -f ${out} ${rscripts}/tls-auth-bacula-dir.conf.in >${rscripts}/tls-auth-bacula-dir.conf


sed -f ${out} ${rscripts}/bacula-fd-2d.conf.in >${rscripts}/bacula-fd-2d.conf
sed -f ${out} ${rscripts}/bacula-sd-2d.conf.in >${rscripts}/bacula-sd-2d.conf
sed -f ${out} ${rscripts}/bacula-dir-2d.conf.in >${rscripts}/bacula-dir-2d.conf
sed -f ${out} ${rscripts}/bconsole-2d.conf.in >${rscripts}/bconsole-2d.conf
sed -f ${out} ${rscripts}/regress-config.in >${rscripts}/regress-config
sed -f ${out} ${rscripts}/swift-plugin-test-bacula-dir.conf.in >${rscripts}/swift-plugin-test-bacula-dir.conf
sed -f ${out} ${rscripts}/cdp-plugin-test-bacula-dir.conf.in >${rscripts}/cdp-plugin-test-bacula-dir.conf
sed -f ${out} ${rscripts}/cdp-plugin-test-bacula-sd.conf.in >${rscripts}/cdp-plugin-test-bacula-sd.conf

sed -f ${out} ${rscripts}/bacula-dir.auth-ldap-plugin.conf.in >${rscripts}/bacula-dir.auth-ldap-plugin.conf
sed -f ${out} ${rscripts}/bconsole.auth-ldap-plugin.conf.in >${rscripts}/bconsole.auth-ldap-plugin.conf

chmod 755 ${rscripts}/regress-config

if test -f ${conf}/bacula-sd.conf ; then
  cp ${conf}/bacula-sd.conf ${tmp}/bac$$
  sed "s% /tmp%${tmp}%g" ${tmp}/bac$$ >${conf}/bacula-sd.conf
fi
chmod 755 ${rscripts}/cleanup-*tape ${rscripts}/cleanup-*drive ${rscripts}/prepare-two-tapes
rm -f ${tmp}/tmp/bac$$
cp ${scripts}/mtx-changer ${tmp}/bac$$
sed "s%^MTX.*$%MTX=${AUTOCHANGER_PATH}%g" ${tmp}/bac$$ >${scripts}/mtx-changer
chmod 755 ${scripts}/mtx-changer ${rscripts}/prepare-fake-autochanger

${rscripts}/set_tape_options
${rscripts}/config_dart

rm -f ${out}
rm -f ${tmp}/bac$$
